// JavaScript Document
$(document).ready(function(){
	
	$(".menu.bot li").hover(function () {
    	$(this).find('ul').slideToggle("fast");
		
  	});
	
	$(".menu.topp li").hover(function () {
    	$(this).find('ul').slideToggle("fast");
		
  	});
	
	if($('#chatbox').is(':visible')){
		var last_id = 0;
		var chat_id_string = '';
		get_chats();
		setInterval(get_latest_chat, 5000); //
		
		function get_latest_chat(){
			var b_id = $('#booking_id').val();
			
			 $.ajax({
                    url: "ajax/get_latest_chat", 
                    type: "POST",
                   	dataType: "json",
                    async: true,
   				    data: "booking_id=" + b_id + "&chat_id_string=" + chat_id_string, 
					success: function(data) {
						if(data){
							 $.each(data, function(i, item) {
   						    if(last_id == item.chat_by){
								//console.log('entered');
								console.log('My ID: '+$('#my_id').val()+' || Last: '+last_id+ ' || '+($('#my_id').val() == last_id) + ' || Message: '+item.message)
								if(!$('#chatbox').find('.cbox').is(':visible')){
									
									if($('#my_id').val() == last_id)
										$('#chatbox').append(assemble_chat(item.message, 'you'));
									else
										$('#chatbox').append(assemble_chat(item.message, 'them'));	
								}
								else {								
									console.log('last appended');
									$('#chatbox .cbox').last().append('<p>'+item.message+'</p>');
								}
							}
							else {
								console.log('Un Matched');
								if($('#my_id').val() == item.chat_by) {
									console.log('me');
									$('#chatbox').append(assemble_chat(item.message, 'you'));
								}
								else
									$('#chatbox').append(assemble_chat(item.message, 'them'));
							}
							
							chat_id_string = chat_id_string+item.id+',';
							last_id = item.chat_by;
							
 						 });	
						}
					},
					error: function(e) {
                    	console.log(e);
                    }
			 });
			
		}
		
		$('#s_send').click(function (){
			if($('#s_msg').val() == '')
				alert('Please place a message');
			else {
				var msg = $('#s_msg').val();
				var booking_id = $('#booking_id').val();
				var chat_by = $('#my_id').val();
				insert_chat(msg, booking_id, chat_by);
							
					
				$('#s_msg').val('');
			}
			return false;
		})
		
		$('#s_msg').keypress(function(event){
			var keycode = (event.keyCode ? event.keyCode : event.which);
			if(keycode == '13'){
				if($('#s_msg').val() == '')
				alert('Please place a message');
				else {
					var msg = $('#s_msg').val();
					var booking_id = $('#booking_id').val();
					var chat_by = $('#my_id').val();
					insert_chat(msg, booking_id, chat_by);
								
						
					$('#s_msg').val('');
				}
				return false;
			}
			event.stopPropagation();
			
		});
		
		function insert_chat(msg, b_id, chatby_id){
	
	 		$.ajax({
                    url: "ajax/chat/", 
                    type: "POST",
                    dataType: "json",
                    async: true,
   				    data: "message=" + msg +"&booking_id="+b_id+"&chat_by="+chatby_id, 
					success: function(data) {
					
						
						if($('#chatbox .cbox').last().hasClass('you'))
							$('#chatbox .cbox').last().append('<p>'+data.msg+'</p>');
						else
							$('#chatbox').append(assemble_chat(data.msg, 'you'));
						
						last_id = data.chat_by;
						chat_id_string = chat_id_string+data.last_chat_id+',';
					},
					error: function(e) {
                    	console.log(e);
                    }
			 });
	
		}
		
		function get_chats(){
			var b_id = $('#booking_id').val();
			
			 $.ajax({
                    url: "ajax/get_chat", 
                    type: "POST",
                    dataType: "json",
                    async: true,
   				    data: "booking_id=" + b_id, 
					success: function(data) {
						 $.each(data, function(i, item) {
   						    if(last_id == item.chat_by){
								//console.log('entered');
								console.log('My ID: '+$('#my_id').val()+' || Last: '+last_id+ ' || '+($('#my_id').val() == last_id) + ' || Message: '+item.message)
								if(!$('#chatbox').find('.cbox').is(':visible')){
									
									if($('#my_id').val() == last_id)
										$('#chatbox').append(assemble_chat(item.message, 'you'));
									else
										$('#chatbox').append(assemble_chat(item.message, 'them'));	
								}
								else {								
									console.log('last appended');
									$('#chatbox .cbox').last().append('<p>'+item.message+'</p>');
								}
							}
							else {
								console.log('Un Matched');
								if($('#my_id').val() == item.chat_by) {
									console.log('me');
									$('#chatbox').append(assemble_chat(item.message, 'you'));
								}
								else
									$('#chatbox').append(assemble_chat(item.message, 'them'));
							}
							
							chat_id_string = chat_id_string+item.id+',';
							last_id = item.chat_by;
							
 						 });
					},
					error: function(e) {
                    	console.log(e);
                    }
			 });
			
		}
		
		
	} /* end if($('#chatbox').is(':visible'))*/
	
	
	
	$("#slider").cycle();
	
	$('#tbl-list, #my_pruchases').dataTable({
		"sPaginationType": "full_numbers",
		"bJQueryUI": true	
	});
	
	if($('.dp').length){
		$('.dp').Zebra_DatePicker({
			direction: true,
			onSelect: function(view, elements) {
				var stylist_id = $('.c_stylist').val();
				var service = $('.service').val();
				
				if(stylist_id != '' && service != ''){
					$.post('ajax/check/', {stylist:stylist_id, adate:$('.dp').val()}, 
						function(data){
							$('.additional_block').html(data);
						}
					);
				}
			}
		});
	}
	
	$('.service').change(function (){
		$('.additional_block').html('');
		var stylist_id = $('.c_stylist').val();
		var aDate = $('.dp').val();
		if(stylist_id != '' && aDate != '' && $(this).val() != ''){
				$.post('ajax/check/', {stylist:stylist_id, adate:aDate}, 
					function(data){
						$('.additional_block').html(data);
					}
				);
			}
	})
	
	$('.c_stylist').change(function (){
		$('.additional_block').html('');
		var service = $('.service').val();
		var aDate = $('.dp').val();
		if(service != '' && aDate != '' && $(this).val() != ''){
				$.post('ajax/check/', {stylist:$(this).val(), adate:aDate}, 
					function(data){
						$('.additional_block').html(data);
					}
				);
			}
	})
	
	
	
	
	$('.s_time').live("click", function(e){
		e.preventDefault();
		$('.t_selected').removeClass('t_selected');
		$(this).addClass('t_selected');
		$('#app_time').val($(this).attr('rel'));
	})
		
	$('#my-appointments').dataTable({
		"sPaginationType": "full_numbers",
		"bJQueryUI": true,
		"aoColumns": [
				null,
				null,
				null,
				null,
                { "bSortable": false },
               { "bSortable": false }
            ],
		"aaSorting": []	
	});
});

function register()
{
	window.location.href = 'http://localhost/juro/home/register';	
}

function confirm_terms()
{

	if($('.accept').is(':checked'))
	{
		return true;
	}
	else {
		alert('Please read and agree to the terms first.');
		return false;
	}
	
}

function assemble_chat(msg, type){
	var chat_class = type == 'you' ? 'you' : 'them';
	var text = type == 'you' ? 'You' : $('#stylist_name').val()+' (Stylist)';
	var chat = '<div class="'+chat_class+' cbox"><span>'+text+'</span><p>'+msg+'</p></div>';
	return chat;
}



